package com.baijiayun.live.ui.toolbox.announcement;

import android.os.Bundle;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.baijiayun.live.ui.R;
import com.baijiayun.live.ui.toolbox.announcement.modelui.BlankTipsFragment;
import com.baijiayun.live.ui.toolbox.announcement.modelui.BlankTipsPresenter;
import com.baijiayun.live.ui.toolbox.announcement.modelui.DoubleAnnFramgent;
import com.baijiayun.live.ui.toolbox.announcement.modelui.DoubleAnnPresenter;
import com.baijiayun.live.ui.toolbox.announcement.modelui.EditAnnContract;
import com.baijiayun.live.ui.toolbox.announcement.modelui.EditAnnFragment;
import com.baijiayun.live.ui.toolbox.announcement.modelui.EditAnnPresenter;
import com.baijiayun.live.ui.toolbox.announcement.modelui.IAnnouncementUI;
import com.baijiayun.live.ui.toolbox.announcement.modelui.NoticeInfo;
import com.baijiayun.live.ui.base.BaseDialogFragment;
import com.baijiayun.live.ui.base.BaseFragment;
import com.baijiayun.live.ui.base.BasePresenter;
import com.baijiayun.live.ui.base.BaseView;
import com.baijiayun.live.ui.utils.QueryPlus;
import com.baijiayun.livecore.models.imodels.IAnnouncementModel;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;

/**
 * Created by Shubo on 2017/4/19.
 */

public class AnnouncementFragment extends BaseDialogFragment implements AnnouncementContract.View {

    private AnnouncementContract.Presenter mPresenter;
    private QueryPlus $;
    private boolean isTeacherView = true;
    private TextWatcher textWatcher;

    private BaseFragment mCurrFragment;
    private BasePresenter mCurrPresenter;

    private IAnnouncementModel mIAnnModel;

    public static AnnouncementFragment newInstance() {

        Bundle args = new Bundle();

        AnnouncementFragment fragment = new AnnouncementFragment();
        fragment.setCancelable(false);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setPresenter(AnnouncementContract.Presenter presenter) {
        super.setBasePresenter(presenter);
        this.mPresenter = presenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.bjy_dialog_announcement;
    }

    @Override
    protected void init(Bundle savedInstanceState, Bundle arguments) {
        super.title(getString(R.string.live_announcement)).editText("");
        hideBackground();
        $ = QueryPlus.with(contentView);

        //初始化默认UI
        mCurrFragment = new BlankTipsFragment();
        bindVP((BlankTipsFragment)mCurrFragment, new BlankTipsPresenter());
        showFragment(mCurrFragment);
        $.id(R.id.tv_announcement_edit_button).visible();
        $.id(R.id.tv_announcement_edit_layout).gone();

        $.id(R.id.tv_announcement_edit_button).clicked(v -> {
            if (!mPresenter.canOperateNoite()) {
                showToast(getString(R.string.live_room_notice_permission_forbid));
                return;
            }
            //跳转到编辑页面
            if (mCurrFragment != null)
                removeFragment(mCurrFragment);
            mCurrFragment = null;
            mCurrPresenter = null;

            mCurrFragment = new EditAnnFragment();
            ((EditAnnFragment) mCurrFragment).setOnAnnEditListener(mAnnEditListener);
            mCurrPresenter = new EditAnnPresenter((EditAnnFragment)mCurrFragment, mPresenter.isGrouping(), mIAnnModel);
            bindVP((EditAnnFragment)mCurrFragment, mCurrPresenter);
            showFragment(mCurrFragment);
            if (mPresenter.getRouter().getLiveRoom().isTeacherOrAssistant()) {
                //公告
                setTitle(1);
            } else {
                //通知
                setTitle(2);
            }
            $.id(R.id.tv_announcement_edit_button).invisible();
            $.id(R.id.tv_announcement_edit_layout).visible();
        });

        $.id(R.id.tv_announcement_edit_cancel).clicked(v -> {
            //取消编辑
            if (mAnnEditListener == null)
                return;
            mAnnEditListener.cannel();
            setTitle(1);
        });

        $.id(R.id.tv_announcement_edit_send).clicked(v -> {
            NoticeInfo info = ((IAnnouncementUI)mCurrPresenter).getNotice();
            if (info == null)
                return;

            if (!TextUtils.isEmpty(info.link) && TextUtils.isEmpty(info.content))
                return;

            if (info.link == null)
                info.link = "";
            if (info.content == null)
                info.content = "";
            mPresenter.saveAnnouncement(info.content, info.link);
            mPresenter.switchUI();
            $.id(R.id.tv_announcement_edit_button).visible();
            $.id(R.id.tv_announcement_edit_layout).gone();
            setTitle(1);
        });

        $.id(R.id.announcement_exit).clicked(v -> {
            dismiss();
        });
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction();
        transaction.add(R.id.fl_announcement, fragment).commit();
    }

    protected void removeFragment(Fragment fragment) {
        if (fragment == null) return;
        FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction();
        transaction.remove(fragment);
        transaction.commitAllowingStateLoss();
    }

    private <V extends BaseView, P extends BasePresenter> void bindVP(V view, P presenter) {
        presenter.setRouter(mPresenter.getRouter());
        view.setPresenter(presenter);
    }

    @Override
    public void editButtonEnable(boolean enable, @StringRes int stringRes) {
        if (enable) {
            //显示
            $.id(R.id.tv_announcement_edit_button).visibility(View.VISIBLE);
            $.id(R.id.tv_announcement_edit_button).text(getResources().getString(stringRes));
        } else {
            //隐藏
            $.id(R.id.tv_announcement_edit_button).visibility(View.GONE);
        }
    }

    @Override
    public void showBlankTips() {

        removeCurrFragment();
        mIAnnModel = null;

        //初始化默认UI
        mCurrFragment = new BlankTipsFragment();
        mCurrPresenter = new BlankTipsPresenter();
        bindVP((BlankTipsFragment)mCurrFragment, mCurrPresenter);
        showFragment(mCurrFragment);
    }

    @Override
    public void setTitle(int titleType) {
        if (titleType == 1) {
            $.id(R.id.tv_announcement_title).text(getResources().getString(R.string.live_announcement));
        } else {
            $.id(R.id.tv_announcement_title).text(getResources().getString(R.string.string_notice_group_title));
        }
    }

    private void removeCurrFragment() {
        if (mCurrFragment != null)
            removeFragment(mCurrFragment);
        mCurrFragment = null;
        mCurrPresenter = null;
    }

    @Override
    public void showCurrUI(int type, int groupId) {

        removeCurrFragment();

        mCurrFragment = new DoubleAnnFramgent();
        if (type == AnnouncementContract.TYPE_UI_TEACHERORASSISTANT) {
            //老师
            mCurrPresenter = new DoubleAnnPresenter((DoubleAnnFramgent)mCurrFragment, AnnouncementContract.TYPE_UI_TEACHERORASSISTANT, groupId);
        } else if (type == AnnouncementContract.TYPE_UI_GROUPTEACHERORASSISTANT) {
            //分组老师
            mCurrPresenter = new DoubleAnnPresenter((DoubleAnnFramgent)mCurrFragment, AnnouncementContract.TYPE_UI_GROUPTEACHERORASSISTANT, groupId);
        } else {
            //学生
            mCurrPresenter = new DoubleAnnPresenter((DoubleAnnFramgent)mCurrFragment, AnnouncementContract.TYPE_UI_STUDENT, groupId);

        }
        bindVP((DoubleAnnFramgent) mCurrFragment, mCurrPresenter);
        showFragment(mCurrFragment);
    }

    @Override
    public void setNoticeInfo(IAnnouncementModel iAnnouncementModel) {
        if (!(mCurrFragment instanceof DoubleAnnFramgent)) {
            mPresenter.switchUI();
        }
        if (mCurrPresenter == null) {
            return;
        }
        if (String.valueOf(mPresenter.getRouter().getLiveRoom().getCurrentUser().getGroup()).equals(iAnnouncementModel.getGroup())) {
            mIAnnModel = iAnnouncementModel;
        }
        ((IAnnouncementUI) mCurrPresenter).setNoticeInfo(iAnnouncementModel);
    }

    @Override
    protected void setWindowParams(WindowManager.LayoutParams windowParams) {
        getDialog().getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_window_common_bg));
        windowParams.width = (int) getResources().getDimension(R.dimen.live_ui_announce_dialog_width);
        windowParams.height = (int) getResources().getDimension(R.dimen.live_ui_announce_dialog_height);
        // 居中
        windowParams.gravity = Gravity.CENTER;
    }

    @Override
    protected boolean hasWindowAnim() {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private EditAnnContract.OnAnnEditListener mAnnEditListener = new EditAnnContract.OnAnnEditListener() {
        @Override
        public void cannel() {
            $.id(R.id.tv_announcement_edit_button).visible();
            $.id(R.id.tv_announcement_edit_layout).gone();
            mPresenter.switchUI();
        }

        @Override
        public void onError() {
            // do nothing
        }

        @Override
        public void Success() {
            // do nothing
        }
    };
}
